IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GrantSuperUserOrSupervisorAccess]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GrantSuperUserOrSupervisorAccess]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/******************************************************************            
* Name:   GrantSuperUserOrSupervisorAccess            
* Purpose:  Grants the person access rights(Super User or Supervisor) with corrsponding userID(Passed in as one of the parameter)			
* NOte: The access will be granted only for users with fwkDomainUserId
	(if the user is not registered as the application user,then the same will be registered as application user.            
* PARAMETERS(IN)            
* Name				Description    
* @partnersLogin	Person for whom the rights have to be granted
* @isSuperUserAccess Determines whether to grant the person with superuser access or supervisor access
		1--> will grant Super User access      
		0--> will grant Supervisor access     
* -------------      -------------------------------------------*/ 
CREATE procedure GrantSuperUserOrSupervisorAccess(@partnersLogin varchar(36),@isSuperUserAccess bit)
AS BEGIN
SET NOCOUNT ON

			
			  Declare @moduleRoleBitMask int
			  Declare @moduleBitMask int
			  Declare @fwkDomainUserID int

			   set @moduleRoleBitMask =null
			   set @moduleBitMask = null
			   set @fwkDomainUserID = null

			If Exists (Select 1 from fwkDomainUser where username =@partnersLogin)
				BEGIN
				   Select @moduleBitMask=moduleBitMask, @moduleRoleBitMask=moduleRoleBitMask, @fwkDomainUserID=fdu.Id from admperson
					inner join fwkDomainUser fdu
					On fdu.Id = admPerson.fwkDomainUserId
					Inner Join FwkApplicationUser fau
					On fau.FwkDomainUserId = fdu.Id
					 where fdu.userName = @partnersLogin 

					If @moduleBitMask is null
						BEGIN
							Set @moduleBitMask = 128						
						END
					ELSE IF @moduleBitMask is not Null
						BEGIN
							if @moduleBitMask & 128 = 0
								BEGIN
									Set @moduleBitMask = @moduleBitMask + 128
								END
						END		  

					IF @moduleRoleBitMask is null 
						BEGIN
							If @isSuperUserAccess =1
								SET @moduleRoleBitMask=2048
							Else If @isSuperUserAccess = 0
								Set @moduleRoleBitMask=512
						END
					ELSE IF @moduleRoleBitMask is not null
						BEGIN
							if (@isSuperUserAccess = 1 and (@moduleRoleBitMask & 2048 =0))
								SET @moduleRoleBitMask = @moduleRoleBitMask + 2048
							ELSE IF (@isSuperUserAccess =0 and (@moduleRoleBitMask & 512 =0))	
								Set @moduleRoleBitMask= @moduleRoleBitMask + 512			
						END					
				
			   if @moduleBitMask is null  
				INSERT INTO FwkApplicationUser(ApplicationBitmask,IsActiveBitmask,FwkDomainUserId,ModuleBitmask,ModuleRoleBitmask)    
				VALUES(4,4,@fwkDomainUserId,@moduleBitMask,@moduleRoleBitMask)--Default values    
			   else if (@moduleBitMask is not null)  
				 Update fwkApplicationuser set ModuleBitMask = @moduleBitMask ,moduleRoleBitMask = @moduleRoleBitMask where fwkDomainUserID=@fwkDomainUserId  
						 
			END 	
		
END 


GO
